/*
 * dir_watcher_base.h
 *
 *  Created on: 2010/07/18
 *      Author: terry
 */

#ifndef DIR_WATCHER_BASE_H_
#define DIR_WATCHER_BASE_H_

#include <string>
#include <vector>
#include <sys/inotify.h>
#include <boost/smart_ptr.hpp>
#include <boost/signals2/signal.hpp>
#include <boost/bind.hpp>
#include "dir_event.h"
#include "common.h"

using namespace std;
using namespace boost;


class dir_watcher_base
{
public:
    typedef signals2::signal<void (shared_ptr<dir_event>)> signal_t;

public:
    dir_watcher_base();
    virtual ~dir_watcher_base();

public:
    virtual void open() throw (op_error);
    virtual void destroy();
    virtual void add_directory(const string& dir, unsigned long mask) throw (op_error);
    virtual void start_watch() throw (op_error);
    virtual void stop_watch();
    virtual size_t watched_items_count();
    virtual vector<string>& watched_items();
    virtual signals2::connection connect(const signal_t::slot_type &subscriber);

protected:
    virtual void on_changed(shared_ptr<dir_event> evt);

private:
    int fd;
    bool opened;
    vector<string> dir_list;
    volatile bool keep_running;
    signal_t sig;
private:
    int event_check();
    int read_events();
};

#endif /* DIR_WATCHER_BASE_H_ */
